package com.humble.humbleojquestionservice.controller;


import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.humble.humbleojcommon.common.BaseResponse;
import com.humble.humbleojcommon.common.ErrorCode;
import com.humble.humbleojcommon.common.ResultUtils;
import com.humble.humbleojcommon.exception.BusinessException;
import com.humble.humbleojcommon.exception.ThrowUtils;
import com.humble.humbleojmodel.model.dto.QuestionSubmit.QuestionSubmitQueryRequest;
import com.humble.humbleojmodel.model.dto.QuestionSubmit.QuestionSubmitRequest;
import com.humble.humbleojmodel.model.entity.QuestionSubmit;
import com.humble.humbleojmodel.model.entity.User;
import com.humble.humbleojmodel.model.enums.RabbitTransportKey;
import com.humble.humbleojmodel.model.vo.QuestionSubmitVO;
import com.humble.humbleojquestionservice.MqProducer.QuestionMessageProducer;
import com.humble.humbleojquestionservice.service.QuestionSubmitService;
import com.humble.humbleojserviceclient.service.JudgeFeignClient;
import com.humble.humbleojserviceclient.service.UserFeignClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.concurrent.CompletableFuture;

/**
 * 帖子收藏接口
 *
 * @author <a href="https://github.com/liyupi">程序员鱼皮</a>
 * @from <a href="https://yupi.icu">编程导航知识星球</a>
 */
@RestController
@RequestMapping("/submit")
@Slf4j
public class QuestionSubmitController {
    @Resource
    QuestionMessageProducer questionMessageProducer;

    @Resource
    private QuestionSubmitService questionSubmitService;

    @Resource
    private UserFeignClient userFeignClient;

    @Resource
    private JudgeFeignClient judgeFeignClient;

    /**
     *
     * @param questionSubmitRequest
     * @param request
     * @return  提交的id
     */
    @PostMapping("/")
    @SentinelResource("questionSubmit")
    public BaseResponse<Long> doQuestionSubmit(@RequestBody QuestionSubmitRequest questionSubmitRequest,
                                               HttpServletRequest request) throws Exception {
        if (questionSubmitRequest == null || questionSubmitRequest.getQuestionId() <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        // 登录才能操作
        final User loginUser = userFeignClient.getLoginUser(request);
        Long submitId = questionSubmitService.doQuestionSubmit(questionSubmitRequest, loginUser);
        //调用service使用异步的方式调用
        // CompletableFuture<Void> future = CompletableFuture.runAsync(() -> {
        //     try {
        //         judgeFeignClient.doJudgeService(submitId);
        //     } catch (Exception e) {
        //         throw new BusinessException(ErrorCode.SYSTEM_ERROR);
        //     }
        // });
        questionMessageProducer.sendMessage(RabbitTransportKey.EXCHANGE_NAME.getValue(),
                RabbitTransportKey.ROUTING_KEY.getValue(),
                submitId.toString());
        return ResultUtils.success(submitId);
    }

    @PostMapping("/list/page/vo")
    public BaseResponse<Page<QuestionSubmitVO>> listQuestionSubmitVOByPage(@RequestBody QuestionSubmitQueryRequest questionSubmitQueryRequest,
                                                                           HttpServletRequest request) {
        long current = questionSubmitQueryRequest.getCurrent();
        long size = questionSubmitQueryRequest.getPageSize();
        User loginUser = userFeignClient.getLoginUser(request);
        // 限制爬虫
        ThrowUtils.throwIf(size > 20, ErrorCode.PARAMS_ERROR);
        Page<QuestionSubmit> questionSubmitPage = questionSubmitService.page(new Page<>(current, size),
                questionSubmitService.getQueryWrapper(questionSubmitQueryRequest));
        Page<QuestionSubmitVO> questionSubmitVOPage = questionSubmitService.getQuestionSubmitVOPage(questionSubmitPage, loginUser);
        return ResultUtils.success(questionSubmitVOPage);
    }

    /**
     * 分页获取当前用户创建的资源列表
     *
     * @param questionSubmitQueryRequest
     * @param request
     * @return
     */
    @PostMapping("/my/list/page/vo")
    public BaseResponse<Page<QuestionSubmitVO>> listMyQuestionSubmitVOByPage(@RequestBody QuestionSubmitQueryRequest questionSubmitQueryRequest,
                                                                 HttpServletRequest request) {
        if (questionSubmitQueryRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        User loginUser = userFeignClient.getLoginUser(request);
        questionSubmitQueryRequest.setUserId(loginUser.getId());
        long current = questionSubmitQueryRequest.getCurrent();
        long size = questionSubmitQueryRequest.getPageSize();
        // 限制爬虫
        ThrowUtils.throwIf(size > 20, ErrorCode.PARAMS_ERROR);
        Page<QuestionSubmit> QuestionSubmitPage = questionSubmitService.page(new Page<>(current, size),
                questionSubmitService.getQueryWrapper(questionSubmitQueryRequest));
        return ResultUtils.success(questionSubmitService.getQuestionSubmitVOPage(QuestionSubmitPage, loginUser));
    }
}
